package datadasetest;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class database extends JFrame {

	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user="hr";
	String db_pwd="hr";
	
	
	Connection conn;
	
	
	
	input1 in = null;//입력
	Select out = null;
	modi mo = null;
	del de = null;
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					database frame = new database();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public database() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 590, 426);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	
		try{
			//데이터베이스 연결 테스트
			//1) 드라이버 로딩
			Class.forName(driver).newInstance();	
		System.out.println("드라이버가 로딩되었습니다");
		//2)Connection 객체 생성(필드에만듬)
		conn=DriverManager.getConnection(url, db_user, db_pwd);
		System.out.println("데이터베이스가 정상적으로 연결됨");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			mnNewMenu.add(getMntmNewMenuItem_3());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("삭제");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(de == null){
						de = new del(conn);						
						de.setBounds(20, 20, 300, 300);						
					}
					contentPane.add(de);
					de.setVisible(true);
				}
				
			});
		}
		return mntmNewMenuItem;
	}
	public void openupdate(){
		if(mo == null){
			mo = new modi(conn,this);						
			mo.setBounds(10, 10, 300, 300);						
		}
		contentPane.add(mo);
		mo.setVisible(true);
mo.toFront();
	}
		
	
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openupdate();	
			
				}	
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("출력(조회)");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(out == null){
						out = new Select(conn,database.this);						
						out.setBounds(30, 30, 450, 300);						
					}
					contentPane.add(out);
					out.setVisible(true);
				}
				
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("입력");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(in == null){
						in = new input1(conn);						
						in.setBounds(40, 40, 450, 300);						
					}
					contentPane.add(in);
					in.setVisible(true);
					in.toFront();
				}
			});
		}
		return mntmNewMenuItem_3;
	}



}

